package com.jeeplus.modules.project.web;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jeeplus.common.utils.WordUtils;
import com.jeeplus.modules.project.entity.BasePartner;
import com.jeeplus.modules.project.entity.BaseProjectInfo;
import com.jeeplus.modules.project.entity.BasepjtInfoBudget;
import com.jeeplus.modules.project.service.BasePartnerService;
import com.jeeplus.modules.project.service.BaseProjectInfoService;
import com.jeeplus.modules.project.service.BasepjtInfoBudgetService;

@Controller
@RequestMapping(value = "${adminPath}/project/export")
public class ExportController {

	@Autowired
	private BaseProjectInfoService baseProjectInfoService;
	@Autowired
	private BasePartnerService partnerService;
	@Autowired
	private BasepjtInfoBudgetService budgetService;
	/**
	 * 导出立项表
	 * @param baseProjectInfo
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequiresPermissions("project:baseProjectInfo:export")
	@RequestMapping(value = "/setup")
	public void setup(BaseProjectInfo baseProjectInfo, HttpServletRequest request, HttpServletResponse response, Model model) {
       try {
           WordUtils.exportMillCertificateWord(request,response,getData(baseProjectInfo),"立项书","template.ftl");
       } catch (IOException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       }
	}
	
	private Map<String, Object> getData(BaseProjectInfo baseProjectInfo) {  
		Map<String, Object> dataMap = new HashMap<String, Object>();
		//动态获取数据
		baseProjectInfo = baseProjectInfoService.get(baseProjectInfo.getId());
		BasePartner bp = new BasePartner();
		bp.setBptInfo(baseProjectInfo);
		List<BasePartner> partners = partnerService.findList(bp);
		StringBuffer sb = new StringBuffer();
		for(BasePartner partner : partners){
			sb.append(partner.getName()+" ");
		}
		BasepjtInfoBudget bd = new BasepjtInfoBudget();
		bd.setBptInfo(baseProjectInfo);
		List<BasepjtInfoBudget> budgets = budgetService.findList(bd);
		double dd = 0.0;
		for(BasepjtInfoBudget gg : budgets){
			dd += gg.getBudget(); 
		}
        dataMap.put("courseName", baseProjectInfo.getCourseName());  
        dataMap.put("projectNo", baseProjectInfo.getProjectNo());  
        dataMap.put("year", new SimpleDateFormat("yyyy").format(new Date()));
        dataMap.put("phone", baseProjectInfo.getHost().getPhone());  
        dataMap.put("email", baseProjectInfo.getHost().getEmail());  
        dataMap.put("host", baseProjectInfo.getHost().getName()); 
        String [] courseType = {"A","B","C","D","E"};
        for(String cc : courseType){
        	dataMap.put(cc, "");
        }
        if(!StringUtils.isEmpty(baseProjectInfo.getCourseType())){
        	dataMap.put(baseProjectInfo.getCourseType().trim(),"√");
        }
        String [] researchType = {"JC","DC","ZH","QT"};
        for(String rt : researchType){
        	dataMap.put(rt, "");
        }
        if(!StringUtils.isEmpty(baseProjectInfo.getResearchType())){
        	dataMap.put(baseProjectInfo.getResearchType().trim(),"√");
        }
        String [] result = {"C1","C2","C3","C4","C5","C6","C7","C8","C9","C10"};
        for(String ss : result){
        	dataMap.put(ss, "");
        }
        if(!StringUtils.isEmpty(baseProjectInfo.getAchieveEndType())){
        	dataMap.put(baseProjectInfo.getAchieveEndType().trim(),"√");
        }
        String [] langs = {"W1","W2","W3","W4","W5"};
        for(String lg : langs ){
        	dataMap.put(lg, "");
        }
        if(!StringUtils.isEmpty(baseProjectInfo.getAchieveLanguage())){
        	dataMap.put(baseProjectInfo.getAchieveLanguage().trim(),"√");
        }
        dataMap.put("fieldCode",baseProjectInfo.getFieldType());
        dataMap.put("fieldName",baseProjectInfo.getFieldTypeName());
        dataMap.put("holders", sb.toString());
        dataMap.put("totalMoney",dd);
        dataMap.put("wordNum", baseProjectInfo.getAchieveWordNum());
        dataMap.put("begin", new SimpleDateFormat("yyyy年MM月dd日").format(baseProjectInfo.getPlanFinishStartTime()));
        dataMap.put("end", new SimpleDateFormat("yyyy年MM月dd日").format(baseProjectInfo.getPlanFinishEndTime()));
        return dataMap;
    }  
	
}
